package com.bii.gelapp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SaveDialog extends Dialog {

	public Context savecontext;
	Button btSave, btCancel;
	EditText etSave;

	public SaveDialog(Context context) {
		super(context);
		try {
			this.savecontext = context;
		} catch (Exception ex) {

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.savedialog);

		WindowManager.LayoutParams wmlp = this.getWindow().getAttributes();
		WindowManager wm = (WindowManager) this.savecontext
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		wmlp.gravity = Gravity.CENTER_VERTICAL;
		wmlp.width = display.getWidth();

		btSave = (Button) findViewById(R.id.btSave);
		btCancel = (Button) findViewById(R.id.btCancel);
		etSave = (EditText) findViewById(R.id.etSave);
		if (MarkerGaborActivity2.filetitle.equals("")) {
			String filename = "marker 1";
			etSave.setText(filename);
		} else {
			String filename = MarkerGaborActivity2.filetitle;
			etSave.setText(filename);
		}

		btSave.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String mstitle = etSave.getText().toString();
				// to be stored:
				MarkerStandard ms = new MarkerStandard();
				ms.title = mstitle;
				// ms.bmp = MarkerGaborActivity.colorBmp;
				// ms.imgsrc = MarkerGaborActivity.picturePath;
				ms.imgsrc = MarkerGaborActivity2.filepath;
				ArrayList<MarkerConnectedComponent> markersconnectedlist = MarkerValueActivity
						.getmarkersconnectedlist();
				ms.markerslist = markersconnectedlist;
				ArrayList standardlist = new ArrayList();
				// 10 september
				try {
					//File mydir = storeImage(MarkerGaborActivity2.colorBmp,
					//		mstitle);
					ms.imgsrc = storeImage(MarkerGaborActivity2.colorBmp,mstitle);

					if (fileExistance("listofstandards") == true) {

						try {
							// arraylist does not change
							standardlist = (ArrayList) InternalStorage
									.readObject(savecontext, "listofstandards");
							if (fileExistance(mstitle) == true) {
								// delete the object
								// InternalStorage.deleteObject(savecontext,
								// mstitle);
								// add the object
								InternalStorage.writeObject(savecontext,
										mstitle, ms);

								// write arraylist
								InternalStorage.writeObject(savecontext,
										"listofstandards", standardlist);
							} else {
								// arraylist add the mstitle
								standardlist.add(mstitle);
								// add the object
								InternalStorage.writeObject(savecontext,
										mstitle, ms);
								// write arraylist
								InternalStorage.writeObject(savecontext,
										"listofstandards", standardlist);
							}

						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					} else {
						standardlist.add(mstitle);
						try {
							InternalStorage.writeObject(savecontext,
									"listofstandards", standardlist);
							InternalStorage.writeObject(savecontext, mstitle,
									ms);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}

					Intent goToStart = new Intent(savecontext,
							StartActivity.class);
					savecontext.startActivity(goToStart);
				} catch (Exception ex) {

				}

			}

		});

		btCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();

			}
		});

	}

	public boolean fileExistance(String key) {
		File file = savecontext.getFileStreamPath(key);
		return file.exists();
	}

	private String storeImage(Bitmap imageData, String filename) {
		String imagepath = "";
		String root = Environment.getExternalStorageDirectory().toString();
		File myDir = new File(root + "/.saved_markers");
		myDir.mkdirs();

		// for nomedia file
		String nmname = "nomedia" + ".nomedia";
		File nomediafile = new File(myDir, "nmname");
		if (nomediafile.exists())
			nomediafile.delete();
		try {
			FileOutputStream out = new FileOutputStream(nomediafile);
			out.flush();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// for marker image
		String fname = filename + ".png";
		File file = new File(myDir, fname);
		if (file.exists())
			file.delete();
		try {
			imagepath = file.toString();
			FileOutputStream out = new FileOutputStream(file);
			imageData.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();

		} catch (Exception e) {
			// e.printStackTrace();

			Toast.makeText(savecontext, e.toString(), Toast.LENGTH_SHORT);
		}
		return imagepath;

	}
}
